package org.fing.edu.uy.admin.managedBean;


import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.fing.edu.uy.admin.factory.ManagerFactory;
import org.fing.edu.uy.admin.utils.FacesUtils;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.primefaces.model.DualListModel;

@ViewScoped
@ManagedBean(name="serviceEquivController")
public class ServiceEquivController implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private DualListModel<DMRegisteredService> services; 	
	
    private List<DMRegisteredService> all;
    private List<DMRegisteredService> allFiltered;
    private List<DMRegisteredService> equiv;  
    private Long serviceID;
    private boolean selectView;    
    private boolean canonical;
	    

    /** Creates a new instance of ServiceController */
    public ServiceEquivController() {
    	serviceID = FacesUtils.getLongRequestParam("id");     
    	if (serviceID != null) {
			selectView = false;
		} else {
			selectView = true;		
		}
    }

    @PostConstruct
    public void init(){
        try {
        	all = ManagerFactory.getServiceManager().getAllVirtualServices();
        	if(serviceID != null){
	        	equiv = ManagerFactory.getServiceManager().getEquivalentsServices(serviceID);
	        	allFiltered = getRest(all,equiv,serviceID);
	        	services = new DualListModel<DMRegisteredService>(allFiltered, equiv);
	        	setCanonical(ManagerFactory.getServiceManager().getVirtualService(serviceID).isCanonicalXSL());
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}           
    }

    
    /**
     * 
     * @param allServ todos lso servicios registrados
     * @param allequiv todos los servicios que son equivalentes
     * @param ServiceID idDel servicio para sacarlo a el mismo.
     * @return Lista con todos los servicios que se pueden asociar.
     */
	private List<DMRegisteredService> getRest(List<DMRegisteredService> allServ,List<DMRegisteredService> allequiv , long ServiceID) {
		List<DMRegisteredService> ret =  new LinkedList<DMRegisteredService>();
		for(DMRegisteredService aux :  allServ){
			if(!allequiv.contains(aux) && aux.getServiceID()!=ServiceID && aux.isCanonicalXSL()){
				ret.add(aux);
			}
		}
		
		return ret;
	}

	public DualListModel<DMRegisteredService> getServices() {
		return services;
	}

	public void setServices(DualListModel<DMRegisteredService> services) {
		this.services = services;
	}

	public List<DMRegisteredService> getAll() {
		return all;
	}

	public void setAll(List<DMRegisteredService> all) {
		this.all = all;
	}

	public List<DMRegisteredService> getEquiv() {
		return equiv;
	}

	public void setEquiv(List<DMRegisteredService> equiv) {
		this.equiv = equiv;
	}
    
	public void save(){
		try {
			List<DMRegisteredService> newEquivBugged= services.getTarget();
			ManagerFactory.getServiceManager().deleteEquivRelation(serviceID);			
			ManagerFactory.getServiceManager().persistEquivVirtualServices(newEquivBugged, serviceID);
			init();
			FacesUtils.addInfo("The data was stored correctly.");
		} catch (Exception ex) {
			ex.printStackTrace();
			FacesUtils.addSev("An error ocurred. See Trace. " + ex.getStackTrace()[0].getMethodName() + " Line number " + ex.getStackTrace()[0].getLineNumber());
		}
	}

	public Long getServiceID() {
		return serviceID;
	}

	public void setServiceID(Long serviceID) {
		this.serviceID = serviceID;
	}

	public boolean isSelectView() {
		return selectView;
	}

	public void setSelectView(boolean selectView) {
		this.selectView = selectView;
	}
	
	public void reload() {
		init();
	}

	public boolean isCanonical() {
		return canonical;
	}

	public void setCanonical(boolean canonical) {
		this.canonical = canonical;
	}

}
